package springdata_CrudRepository.pager;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author CHUNLONG.LUO
 * @email 584614151@qq.com
 * @version 1.0
 */
public class PageTag extends TagSupport {
	
	//代表当前页码
	private int pageIndex;
	//每页显示的记录数
	private int pageSize;
	//总记录数
	private int totalNum;
	//提交的地址
	private String submitUrl; //index.action?pageIndex={0}
	//分页样式
	private  String pageStyle ="black";
	



	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub

		try {
			//通过 JspWriter往页面中写出数据
			JspWriter jspWriter = this.pageContext.getOut();
			
			//创建StringBuffer用于拼装页码相关信息
			StringBuffer  str = new StringBuffer();
			
			//计算总页数                        
			int totalPageNum = totalNum % pageSize == 0?totalNum / pageSize : (totalNum / pageSize) + 1;
			
			//定义跳转地址
			String jumpUrl = "";
			
			//用于拼装 页码信息
			StringBuffer pager = new StringBuffer();
			//当前页码是第一页
			if(pageIndex == 1) {
				pager.append("<span class='disabled'>上一页</span>");
				
				
				//处理中间页码   1 2 3 4 5 6 7 8 9 ... 100
				calcMiddlePage(pager,totalPageNum);
				
				//如果总共只有一页 ，那么下一页也不能点击
				if(totalPageNum == 1) {
					pager.append("<span class='disabled'>下一页</span>");
				}else {
					jumpUrl = this.submitUrl.replace("{0}", String.valueOf(this.pageIndex+1));
					pager.append("<a href='"+jumpUrl+"'>下一页</a>");
				}
				
			}else if(pageIndex == totalPageNum) {
				//当前页码是尾页
				jumpUrl = this.submitUrl.replace("{0}", String.valueOf(this.pageIndex-1));
				pager.append("<a href='"+jumpUrl+"'>上一页</a>");
				
				//处理中间页码   1 2 3 4 5 6 7 8 9 ... 100
				calcMiddlePage(pager,totalPageNum);
				
				
				pager.append("<span class='disabled'>下一页</span>");
				
			}else {
				
				//submitUrl:index.action?pageIndex={0}
				//当前页码在中间
				jumpUrl = this.submitUrl.replace("{0}", String.valueOf(this.pageIndex-1));
				pager.append("<a href='"+jumpUrl+"'>上一页</a>");
				
				
				//处理中间页码   1 2 3 4 5 6 7 8 9 ... 100
				calcMiddlePage(pager,totalPageNum);
				
				jumpUrl = this.submitUrl.replace("{0}", String.valueOf(this.pageIndex+1));
				pager.append("<a href='"+jumpUrl+"'>下一页</a>");
			}
			
			//计算每页的开始以及结束行号
			int startSize = (this.pageIndex - 1) * this.pageSize + 1;
			int endSize = this.pageIndex == totalPageNum ? this.totalNum : this.pageIndex * this.pageSize;
		
		    str.append("<table class='"+this.pageStyle+"' style='align:center' width='100%'><tr><td>"+pager.toString()+" 跳转到<input type='text' size='4' id='jumpNum'/><input type='button' value='跳转' id='jumpBut'/></td></tr>");
			
			str.append("<tr><td>总共<font color='red'>"+this.totalNum+"</font>条记录,当前显示"+startSize+"-"+endSize+"条记录</td></tr></table>");
			
			
			
			
			
			str.append("<script type='text/javascript'>");
			
			str.append("document.getElementById('jumpBut').onclick = function(){");
			str.append("var value = document.getElementById('jumpNum').value;");
			str.append("if(!/^[1-9]\\d*$/.test(value)||value > "+this.totalNum+"){");
			str.append("alert('请输入[1-"+totalPageNum+"]之间的页码值！');");
			str.append("}else{");
			// index.action?pageIndex = {0}
			str.append("var submiturl = '"+this.submitUrl+"';");
			str.append("submiturl = submiturl.replace('{0}',value);");
			str.append("window.location = submiturl;");
			
			str.append("}");
			
			str.append("}");
			str.append("</script>");
			
			//将str写出至jsp页面
			jspWriter.write(str.toString());
			
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		return super.doStartTag();
	}

	//计算中间页码
	public void calcMiddlePage(StringBuffer str, int totalPageNum) {
		// TODO Auto-generated method stub
		String jumpUrl = "";
		//1、如果总页码数小于等于10，直接将所有的页码都显示 不要 ...    1 2 3 4 5 6 7 8 9 10
		if(totalPageNum <= 10) {
			for(int i=1;i<=totalPageNum;i++) {
				
				//当前页码不需要跳转
				if(i==this.pageIndex) {
					str.append("<span class='current'>"+i+"</span>");
				}else {
					jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(i));
					str.append("<a href='"+jumpUrl+"'>"+i+"</a>");
				}
			}
			
		}else if(this.pageIndex <= 8) {
			//2、当前页码靠近首页   1 2 3 4 5 6 7 8 9 ... 100
            for(int i=1;i<=9;i++) {
            	//当前页码不需要跳转
				if(i==this.pageIndex) {
					str.append("<span class='current'>"+i+"</span>");
				}else {
					jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(i));
					str.append("<a href='"+jumpUrl+"'>"+i+"</a>");
				}
            }
            
            //拼装...
            str.append("...");
            
            //拼装尾页
            jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(totalPageNum));
			str.append("<a href='"+jumpUrl+"'>"+totalPageNum+"</a>");
			
			
		}else if(this.pageIndex + 8 >= totalPageNum){
			    //当前页码靠近尾页  1 ... 91 92 93 94 95 96 97 98 99 100 
			    jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(1));
				str.append("<a href='"+jumpUrl+"'>"+1+"</a>");
				
				 //拼装...
	            str.append("...");
			
			  for(int i=totalPageNum - 9;i<=totalPageNum;i++) {
	            	//当前页码不需要跳转
					if(i==this.pageIndex) {
						str.append("<span class='current'>"+i+"</span>");
					}else {
						jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(i));
						str.append("<a href='"+jumpUrl+"'>"+i+"</a>");
					}
	            }
		}else {
			
			//当前页码在中间  1 ... 44 45 46 47 48 49 50 51 52  ... 100
		    jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(1));
			str.append("<a href='"+jumpUrl+"'>"+1+"</a>");
			 //拼装...
            str.append("...");
            
            for(int i=this.pageIndex - 4;i<=this.pageIndex + 4;i++) {
            	//当前页码不需要跳转
				if(i==this.pageIndex) {
					str.append("<span class='current'>"+i+"</span>");
				}else {
					jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(i));
					str.append("<a href='"+jumpUrl+"'>"+i+"</a>");
				}
            }
          //拼装...
            str.append("...");
            
            //拼装尾页
            jumpUrl  =this.submitUrl.replace("{0}", String.valueOf(totalPageNum));
			str.append("<a href='"+jumpUrl+"'>"+totalPageNum+"</a>");
		}
		
		
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		System.out.println("pageIndex:"+pageIndex);
		if(pageIndex == 0) {
			pageIndex = 1;
		}
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public String getSubmitUrl() {
		return submitUrl;
	}

	public void setSubmitUrl(String submitUrl) {
		this.submitUrl = submitUrl;
	}

	public String getPageStyle() {
		return pageStyle;
	}

	public void setPageStyle(String pageStyle) {
		this.pageStyle = pageStyle;
	}

	  
	
}
